package com.example.taskschedule.domain.task;

import com.example.taskschedule.infrastructure.threadPool.StatisticsCollector;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 模拟1-3秒的任务处理
 */
@Slf4j
@Data
public class MyTask implements Runnable{
    private final int taskId;
    private final long startTime;
    public MyTask(int taskId) {
        this.taskId = taskId;
        this.startTime = System.currentTimeMillis();
    }
    @Override
    public void run() {
        try {
            long duration = (long) (Math.random() * 3000 + 1000);
            Thread.sleep(duration);
            log.info("任务ID：{}，执行线程：{}，耗时：{}",taskId, Thread.currentThread().getName(), duration);
            StatisticsCollector.recordTaskCompletion(duration);
        } catch (Exception e) {
            log.error("taskId: {}, error: {}", taskId, e.getMessage());
            Thread.currentThread().interrupt();
        }
    }
}
